package com.techbrand.lb7.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.techbrand.lb7.dao.CustomerDao;
import com.techbrand.lb7.domain.Customer;

public class HibernateCustomerDao extends HibernateDaoSupport implements CustomerDao {

	/* (non-Javadoc)
	 * @see com.techbrand.lb7.dao.impl.CustomerDao#getAll()
	 */
	@SuppressWarnings("unchecked")
	public List<Customer> getAll() {
		return getHibernateTemplate().find("from Customer");
	}
	
	/* (non-Javadoc)
	 * @see com.techbrand.lb7.dao.impl.CustomerDao#save(com.techbrand.lb7.domain.Customer)
	 */
	public void save(Customer customer) {
		getHibernateTemplate().saveOrUpdate(customer);
	}
	
	/* (non-Javadoc)
	 * @see com.techbrand.lb7.dao.impl.CustomerDao#delete(java.lang.Long)
	 */
	public void deleteById(Long customerId) {
		Customer cust = getById(customerId);
		getHibernateTemplate().delete(cust);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.techbrand.lb7.dao.CustomerDao#deleteList(java.util.List)
	 */
	public void deleteList(List<Long> customerIds) {
		List<Customer> customerList = getList(customerIds);
		getHibernateTemplate().deleteAll(customerList);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.techbrand.lb7.dao.CustomerDao#getList(java.util.List)
	 */
	@SuppressWarnings("unchecked")
	public List<Customer> getList(List<Long> customerIds) {
		return getHibernateTemplate().findByNamedParam("from Customer cust " +
				"where cust.customerId in (:ids)", "ids", customerIds);
	}
	
	/* (non-Javadoc)
	 * @see com.techbrand.lb7.dao.impl.CustomerDao#getById(java.lang.Long)
	 */
	public Customer getById(Long customerId) {
		return (Customer) getHibernateTemplate().load(Customer.class, customerId);
	}
}

